package com.design.prototypepattern.deap;

import java.io.*;
import java.util.List;

/**
 * @auther LiZiBa
 * @date 2020/8/9 0:09
 * @description:
 **/
public class Course implements Cloneable, Serializable {

    private String name;
    private String time;
    private List<String> teacher;

    public Course() {
    }

    public Course(String name, String time, List<String> teacher) {
        this.name = name;
        this.time = time;
        this.teacher = teacher;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public List<String> getTeacher() {
        return teacher;
    }

    public void setTeacher(List<String> teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "Course{" +
                "name='" + name + '\'' +
                ", time='" + time + '\'' +
                ", teacher=" + teacher +
                '}';
    }

    @Override
    public Course clone() throws CloneNotSupportedException {
        return (Course) super.clone();
    }
    
    /**
     *深克隆
     */
    public Course deapClone(){

        try{
            /**
             * 把对象写入内存
             * 代码的含义是通过调用ObjectOutputStream类的writeObject()方法将一个对象写入到一个字节数组输出流ByteArrayOutputStream中，其中的字节数组其实就是内存的一个缓冲区。
             */
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream ous = new ObjectOutputStream(baos);
            ous.writeObject(this);

            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);

            // 对象反序列和之后通过反射new Instance() 获取新的对象
            ois.close();
            bais.close();
            ous.close();
            baos.close();
            return (Course)ois.readObject();

        }catch (Exception e){

            e.printStackTrace();
        }finally {

        }
        return null;
    }
}
